﻿using Log.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Z13qu.Code;
using Z13qu.Model;
using Z13qu.Service;

namespace Z13qu.Web.Controllers
{
    public class LoginController : Controller
    {
        Sys_UserService userService = new Sys_UserService();

        // GET: Login
        public ActionResult Index()
        {
            var users = userService.FindEntity("619e83af-0e17-43db-b60e-49c98f138bd5");
            //users.Email = "ttt@163.com";
            //userService.Update(users);
            return View();
        }

        /// <summary>
        /// 生成验证码图片
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }

        [HttpPost]
        public JsonResult CheckLogin(string username, string password, string code)
        {
            AjaxResult result = new AjaxResult() { success = false, message = "登录失败,用户名或密码错误" };
            Sys_Log log = new Sys_Log();
            log.Account = username;
            log.ModuleName = "系统登录";
            log.IPAddress = Net.Ip;
            log.IPAddressName = Net.GetLocation(log.IPAddress);

            Sys_User user = null;
            try
            {
                if (Session["verifycode"].IsEmpty() || Md5Helper.Create(code.ToLower()) != Session["verifycode"].ToString())
                {
                    throw new Exception("验证码错误，请重新输入");
                }

                //Sys_User user = new UserApp().CheckLogin(username, password);
                user = userService.CheckLogin(username, password);
                if (user != null)
                {
                    OperatorModel operatorModel = new OperatorModel();
                    operatorModel.UserId = user.Id;
                    operatorModel.Account = user.Account;
                    operatorModel.RealName = user.RealName;
                    operatorModel.RoleId = user.RoleId;
                    operatorModel.LoginIPAddress = log.IPAddress;
                    operatorModel.LoginIPAddressName = log.IPAddressName;
                    operatorModel.LoginTime = DateTime.Now;
                    operatorModel.LoginToken = DESEncrypt.Encrypt(Guid.NewGuid().ToString());
                    operatorModel.IsSystem = user.IsAdministrator.ToBool();
                    OperatorProvider.Provider.AddCurrent(operatorModel);

                    log.RealName = user.RealName;
                    log.Result = true;
                    log.Description = "登录成功";
                    LogHelper.Info(log);

                    result.success = true;
                    result.message = "登录成功";
                }
            }
            catch (Exception ex)
            {
                log.Result = false;
                log.Description = "登录失败，" + ex.Message;
                LogHelper.Error(log);

                result.success = false;
                result.message = ex.Message;
            }
            return Json(result);
        }
    }
}